package org.loadsavant.task;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * This immutable class represents the metadata of a task  and contains a 
 * reference to the implementation (i.e. class) of the task.  It is used 
 * by the UI to display documentation and request values for each attribute.
 * It is used by the runtime to instantiate a new task and initialize the
 * fields.
 * 
 * @author Matt Betzel mattbetzel@gmail.com
 */
public class TaskDefinition {
	private Class<?> taskClass;
	private String name;
	private String description;
	private boolean background;
	
	private Set<TaskAttributeDefinition> attributeDefinitions;
	
	public TaskDefinition(Class<?> taskClass, String name, String description,
			boolean background, Set<TaskAttributeDefinition> attributeDefinitions) {
		this.taskClass = taskClass;
		this.name = name;
		this.description = description;
		this.background = background;
		this.attributeDefinitions = 
			new HashSet<TaskAttributeDefinition>(attributeDefinitions);
	}

	public Class<?> getTaskClass() {
		return taskClass;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public boolean isBackground() {
		return background;
	}
	
	public Set<TaskAttributeDefinition> getAttributeDefinitions() {
		return Collections.unmodifiableSet(attributeDefinitions);
	}
	
	public TaskAttributeDefinition getAttributeDefinition(String name) {
		for (TaskAttributeDefinition defintion : attributeDefinitions) {
			if (defintion.getName().equals(name)) {
				return defintion;
			}
		}
		
		return null;
	}

	@Override
	public String toString() {
		return getName();
	}
}
